package cn.wolfcode.car.business.service.impl;


import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.mapper.StatementItemMapper;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.query.StatementItemQuery;
import cn.wolfcode.car.business.query.StatementQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
@Transactional
public class StatementItemServiceImpl implements IStatementItemService {

    @Autowired
    private StatementItemMapper statementItemMapper;
    @Autowired
    private IStatementService statementService;
    @Autowired
    private IAppointmentService appointmentService;
    @Override
    public TablePageInfo<StatementItem> query(StatementItemQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<StatementItem>(statementItemMapper.selectForList(qo));
    }



    @Override
    public StatementItem get(Long id) {
        return null;
    }

    @Override
    public void save(StatementItem statementItem) {

    }

    @Override
    public void update(StatementItem statementItem) {

    }

    @Override
    public void deleteBatch(String ids) {

    }

    @Override
    public List<Statement> list() {
        return null;
    }

    @Override
    @Transactional
    public void saveItems(List<StatementItem> items) {
        StatementItem tempItems = items.remove(items.size() - 1);
        BigDecimal price = tempItems.getItemPrice();
        Long statementId = tempItems.getStatementId();
        Statement statement = statementService.get(statementId);
        if (Statement.STATUS_PAID.equals(statement.getStatus())) {
            throw new BusinessException("只有消费中才能保存明细");
        }
        statementItemMapper.deleteByPrimaryKey(statementId);
        BigDecimal totalAmount = new BigDecimal("0.0");
        BigDecimal totalQuantity = new BigDecimal("0");
        for (StatementItem item : items) {
            statementItemMapper.insert(item);
            totalAmount = totalAmount.add(item.getItemQuantity().multiply(item.getItemPrice()));
            totalQuantity = totalQuantity.add(item.getItemQuantity());
        }
        if (price.compareTo(totalAmount) > 0) {
            throw new BusinessException("非法操作,折扣价不能大于消费总金额");
        }
        statementService.updateAmount(statementId,totalAmount,totalQuantity,price);
    }

    @Override
    @Transactional
    public void payStatement(Long statementId) {
        Statement statement = statementService.get(statementId);
        if (Statement.STATUS_PAID.equals(statement.getStatus())) {
            throw new BusinessException("已支付,请勿重复支付");
        }
        statementService.updatePayStatus(statementId,Statement.STATUS_PAID, ShiroUtils.getUserId());
        if (statement.getAppointmentId() != null) {
            appointmentService.updatePayStatus(statement.getAppointmentId(), Appointment.STATUS_SETTLE);
        }
    }

    @Override
    public void delete(Long id) {
        statementItemMapper.delete(id);
    }
}
